
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
const api = require('../../utils/api.js')
const app = getApp()
Page({
  data: {
    tabs: ["可使用", "已使用", "已过期"],
    activeIndex: '0',
    sliderOffset: 0,
    sliderLeft: 0,
    userId: '',
    couponList: [],
    page: 1,
    end: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      userId: options.userId

    })
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
    this.getCouponList('0');

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      couponList: [],
      page: 1,
      end: false
    })
    this.getCouponList(this.data.activeIndex, 1);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var scroll_page = this.data.page;
    if (!this.data.end) {
      scroll_page++;
      this.setData({
        page: scroll_page
      })
      this.getCouponList(this.data.activeIndex, scroll_page, )
    } else {
      return wx.showToast({
        title: '没有更多优惠券了',
        icon: 'none',
        duration: 1500
    })
      
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id,
      page: 1,
      couponList: [],
      end:false
    });

    this.getCouponList(e.currentTarget.id)
  },
  /* 获取优惠券 */
  getCouponList: function (status, page) {
    wx.stopPullDownRefresh()
    var that = this;
    var lists = this.data.couponList;
    var type = 0 + status;
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function () {
      api.post('funds/user/voucher/list', {
        page: page || 1,
        pageSize: 10,
        userId: that.data.userId || wx.getStorageSync('user').userId+'',
        status: type,
        shopId: app.globalData.shopId + ''
      }).then(res => {
        if (res.data.success && res.data.data) {
          res.data.data.list.map(item => {
            var coupon_item = {};
            coupon_item.title = item.name;
            coupon_item.date = item.limitDay;
            coupon_item.count = parseInt(item.discount*1000)/100;
            coupon_item.status = status;
            coupon_item.limitUsed = item.limitTime;
            coupon_item.disType = item.disType
            if(typeof(item.maxAmount)==="number"){
              coupon_item.maxAmount = item.maxAmount/100;
            }
            
            coupon_item.minAmount = item.minAmount/100;
            lists.push(coupon_item)
          })
          that.setData({
            couponList: lists
          })
          wx.hideLoading()
        }

        if(that.data.page*10 >= res.data.data.total){
          that.setData({
            end: true
          })
        }
        // if(res.data.data.total==='0'){
        //   return wx.showToast({
        //     title: '暂无记录',
        //     icon: 'none',
        //     duration: 1500
        // })
        // }
      })

    }, 500)

  }
})